<template>
  <div>
    <!-- 搜索区域 -->
    <div class="search_box">
      <el-form ref="searchFormRef" :model="queryInfo" label-width="120px" class="search-box">
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item prop="goods_name" label="商品名称:">
              <el-input size="large" v-model.trim="queryInfo.goods_name" placeholder="商品名称" :prefix-icon="Search"
                @change="searchTable"></el-input>
            </el-form-item>
          </el-col>

<!--          <el-col :span="6">-->
<!--            <el-form-item prop="dates" label="日期范围:">-->
<!--              <el-date-picker v-model="queryInfo.dates" type="daterange" size="large" start-placeholder="开始日期"-->
<!--                end-placeholder="结束日期" value-format="YYYY-MM-DD HH:mm:ss" @change="changeDate" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->

<!--          <el-col :span="6">-->
<!--            <el-form-item prop="status" label="订单状态:">-->
<!--              <Select width="240" placeholder="请选择订单状态" v-model="queryInfo.status" :data="statusData"-->
<!--                @change="searchTable" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :span="6">
            <div class="flex justify-end align-center">
              <el-button type="info" :icon="RefreshRight" size="large" @click="resetTable">重置</el-button>
              <el-button type="primary" @click="openCreatePayLinkModal" size="large">生成代付链接</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="margin-top">
      <el-table ref="multipleTableRef" :data="listData" style="width: 100%">
        <el-table-column type="selection" width="55" />

        <el-table-column property="产品图片" label="产品图片">
          <template #default="scope">
            <div class="box">
              <el-image :src="scope.row.head_img[0]" />
            </div>
          </template>
        </el-table-column>

        <el-table-column property="goods_name" label="产品名称" />

        <el-table-column property="shop_title" label="店铺名称" />

        <el-table-column property="goods_money" label="原价">
          <template #default="scope">
            <div v-for="item in scope.row.prices" :key="item.id">
              <div class="flex align-center">
                <div>{{ item.money_name }}</div>

                <div class="margin-left-xs">${{ item.money }}</div>

                <div class="margin-left">折扣价: {{ item.discount_money }}</div>
              </div>
            </div>

            <div></div>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200">
          <template #default="scope">
            <div class="flex align-center">
              <el-button class="margin-left-xs" size="small" type="primary"
                @click="openShoppingCartModal(scope.row)">加入购物车</el-button>
            </div>

            <!-- <div class="flex align-center">
              <el-button class="margin-left-xs" size="small" type="primary"
                @click="openLinkGeneratedForm(scope.row)">生成链接</el-button>
            </div> -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <div class="margin-top">
      <Pagination :total="total" @change="changePage" />
    </div>

    <!-- 生成链接弹窗 -->
    <LinkGeneratedModal ref="linkGeneratedModalRef" />

    <ShoppingCartModal ref="shoppingCartModalRef" />

    <!-- 生成代付链接 -->
    <CreatePayLinkModal ref="createPayLinkModalRef" @confirm="handleConfirmCreateLink" />

    <ShowPayLinkModal ref="showPayLinkModalRef" />
  </div>
</template>

<script lang="ts" setup>
import { useRoute } from 'vue-router'
import { GoodsDataListApi } from '@/api/index'
import useTableHook from '@/hooks/useTableHook'
import Select from '@/components/admin/Select.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { RefreshRight } from '@element-plus/icons-vue'
import Pagination from '@/components/admin/Pagination.vue'
import { ref, reactive, computed, toRef, onMounted, watch } from 'vue'
import LinkGeneratedModal from '@/components/distributor/products/LinkGeneratedModal.vue'
import ShoppingCartModal from '@/components/distributor/products/ShoppingCartModal.vue'
import CreatePayLinkModal from '@/components/distributor/products/CreatePayLinkModal.vue'
import ShowPayLinkModal from '@/components/admin/ShowPayLinkModal.vue'

const statusData = [
  {
    label: '全部状态',
    value: ''
  },
  {
    label: '待发货',
    value: 0,
    color: '#f4f4f5'
  },
  {
    label: '发货中',
    value: 1,
    color: '#409eff'
  },
  {
    label: '代签收',
    value: 2,
    color: '#e6a23c'
  },
  {
    label: '已签收',
    value: 3,
    color: '#66c03a'
  },
  {
    label: '结束订单',
    value: 4,
    color: '#909399'
  },
  {
    label: '取消订单',
    value: 5,
    color: '#f56c6c'
  }
]
const payStatusData = [
  {
    label: '待付款',
    value: 0,
    color: ''
  },
  {
    label: '付款成功',
    value: 2,
    color: '#409eff'
  },
  {
    label: '取消付款',
    value: 3,
    color: '#e6a23c'
  },
]

const showPayLinkModalRef = ref()

const handleConfirmCreateLink = (record) => {
  showPayLinkModalRef.value.open({
    data: record.data
  })
}

const createPayLinkModalRef = ref()

const openCreatePayLinkModal = () => {
  createPayLinkModalRef.value.open()
}

const shoppingCartModalRef = ref()

const openShoppingCartModal = (record) => {
  shoppingCartModalRef.value.open({
    data: record
  })
}

const queryInfo = ref({
  order_no: '',
  dates: [],
  start_time: '',
  end_time: '',
  status: '',
  page: 1,
  limit: 10,
})

const searchFormRef = ref<FormInstance>()

const linkGeneratedModalRef = ref()

const openLinkGeneratedForm = (record) => {
  linkGeneratedModalRef.value.open({
    data: record
  })
}

// 重置表格
const resetTable = () => {
  searchFormRef.value.resetFields()

  queryInfo.value.start_time = ''

  queryInfo.value.end_time = ''

  console.log(1111, queryInfo.value)

  searchTable()
}

const changeDate = (record) => {
  queryInfo.value.start_time = queryInfo.value.dates[0]
  queryInfo.value.end_time = queryInfo.value.dates[1]
  searchTable()
}

onMounted(() => {
  initTable()
})

const {
  total,
  listData,
  searchTable,
  initTable,
  changePage,
} = useTableHook(queryInfo.value, GoodsDataListApi)
</script>

<style lang="scss" scoped>
/* 头部区域样式 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 15px;
}

.title-section {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  color: #2c3e50;
}

.merchant-name {
  color: #e74c3c;
}

.back-link {
  display: inline-flex;
  align-items: center;
  font-size: 14px;
  color: #7f8c8d;
  text-decoration: none;
  transition: color 0.3s;
  margin-top: 5px;
}

.back-link:hover {
  color: #e74c3c;
}

.back-link i {
  margin-right: 5px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.btn-primary {
  background-color: #e74c3c;
  color: white;
  border: none;
  padding: 10px 15px;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  font-weight: 600;
  transition: background-color 0.3s;
}

.btn-primary:hover {
  background-color: #c0392b;
}

.btn-primary i {
  margin-right: 5px;
}
</style>
